package com.asyf.demo;

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@MapperScan("com.asyf.demo.mapper")
public class FlowableApplication {

    /**
     * flowable
     * 用户手册:https://tkjohn.github.io/flowable-userguide/
     * <p>
     * 在线绘图：https://bpmn.52itstyle.vip/
     *
     * @param args
     */
    public static void main(String[] args) {
        SpringApplication.run(FlowableApplication.class, args);
        System.out.println("http://localhost:8080/doc.html");
    }

    /**
     * 测试部署是否生效。CommandLineRunner是一个特殊的Spring bean，在应用启动时执行
     * (注意：表会自动生成)
     * <p>
     * 会得到这样的输出：
     * Number of process definitions : 3
     * Number of tasks : 0
     * Number of tasks after process start: 1
     */
    @Bean
    public CommandLineRunner init(final RepositoryService repositoryService,
                                  final RuntimeService runtimeService,
                                  final TaskService taskService) {

        return new CommandLineRunner() {
            @Override
            public void run(String... strings) throws Exception {
                System.out.println("Number of process definitions : "
                        + repositoryService.createProcessDefinitionQuery().count());
                System.out.println("Number of tasks : " + taskService.createTaskQuery().count());
                runtimeService.startProcessInstanceByKey("oneTaskProcess");
                System.out.println("Number of tasks after process start: "
                        + taskService.createTaskQuery().count());
            }
        };
    }
}
